package com.zchat.android;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ChatManagerListener;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;

import com.zchat.android.app.BaseActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * 单人聊天室
 * @author Oliverzhu
 * @date 2012/6/24
 * @version 1.0
 */
public class ChatActivity extends BaseActivity {
	private Button send;
	private TextView record;
	private EditText message;

	private Chat newChat;

	private MessageListenerEx ml = new MessageListenerEx();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.single_chat);
		
		super.onCreate(savedInstanceState);
		
		
		ChatManager chatmanager = zchat.getXMPPConnection().getChatManager();

		// get user
		Intent intent = getIntent();
		String user = intent.getStringExtra("user");
		System.out.println("user:" + user);
		
		// 监听被动接收消息，或广播消息监听器(要在事件发生之前注册监听)
//		 chatmanager.addChatListener(new ChatManagerListenerEx());
		 
		// 对指定用户的域建议单人聊天室
		if(zchat.getCache().get(user) != null)
		{
			newChat = (Chat) zchat.getCache().get(user);
		}else
		{
			newChat = chatmanager.createChat(user, null);
			zchat.getCache().put(user, newChat);
		}
		 
		 
		
		//直接添加消息处理监听
//		newChat.addMessageListener(ml);

		// send message
		try {
			newChat.sendMessage("im bird man");

		} catch (XMPPException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public class ChatManagerListenerEx implements ChatManagerListener {

		@Override
		public void chatCreated(Chat chat, boolean arg1) {
			chat.addMessageListener(ml);
		}

	}

	public class MessageListenerEx implements MessageListener {

		@Override
		public void processMessage(Chat arg0, Message message) {
			String result = message.getFrom() + ":" + message.getBody();
			System.out.println("ActivityChat-"+result);
			android.os.Message msg = new android.os.Message();
			msg.what = 0;
			Bundle bd = new Bundle();
			bd.putString("msg", result);
			msg.setData(bd);
			handler.sendMessage(msg);

		}
	}

	public Handler handler = new Handler() {

		@Override
		public void handleMessage(android.os.Message msg) {

			switch (msg.what) {
			case 0: {
				String result = msg.getData().getString("msg");
				record.setText(record.getText() + "\n" + result);
			}
				break;
			default:
				break;
			}
		}
	};
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			newChat.removeMessageListener(ml);
			System.out.println("exit chat");
			finish();
		default:
			break;
		}
		return true;
	}
	@Override
	protected void onDestroy() {
		newChat.removeMessageListener(ml);
		super.onDestroy();
	}
	@Override
	protected void initComponents() {
		send = (Button) findViewById(R.id.send);
		record = (TextView) findViewById(R.id.record);
		message = (EditText) findViewById(R.id.message);
	}
	@Override
	protected void initDatas() {
		
	}
	@Override
	protected void setComponentListeners() {
		send.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				try {
					newChat.sendMessage(message.getText().toString());
				} catch (XMPPException e) {
					e.printStackTrace();
				}
			}

		});
	}
}
